﻿using System;
using System.Diagnostics;
using System.Windows.Input;

namespace HY.Common
{
    /// <summary>
    /// 不需要接收参数的RelayCommand
    /// </summary>
    public class RelayCommand : ICommand
    {
        #region 字段

        readonly Func<bool> _canExecute;

        readonly Action _execute;

        #endregion

        #region 构造函数

        public RelayCommand(Action execute)
            : this(execute, null)
        {

        }

        public RelayCommand(Action execute, Func<bool> canExecute)
        {
            _execute = execute ?? throw new ArgumentNullException(nameof(execute));
            _canExecute = canExecute;
        }

        #endregion

        #region ICommand的成员

        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested += value;
            }

            remove
            {
                if (_canExecute != null)
                    CommandManager.RequerySuggested -= value;
            }
        }

        [DebuggerStepThrough]

        public Boolean CanExecute(Object parameter)
        {
            return _canExecute == null ? true : _canExecute();
        }

        public void Execute(Object parameter)
        {
            _execute();
        }

        #endregion
    }

    /// <summary>
    /// RelayCommand，Execute带参数
    /// 个人修改，CanExecute可同步接收参数，也可选择不接收参数
    /// </summary>
    public class RelayCommand<T> : ICommand
    {
        readonly Action<T> _execute = null;
        readonly Predicate<T> _canExecute = null;
        readonly Func<bool> _canExecute2 = null;

        public RelayCommand(Action<T> execute)
        {
            _execute = execute ?? throw new ArgumentNullException(nameof(execute));
        }

        /// <summary>
        /// CanExecute接收参数使用
        /// </summary>
        /// <param name="execute">The execution logic.</param>
        /// <param name="canExecute">The execution status logic.</param>
        public RelayCommand(Action<T> execute, Predicate<T> canExecute)
        {
            _execute = execute ?? throw new ArgumentNullException(nameof(execute));
            _canExecute = canExecute;
        }
        /// <summary>
        /// CanExecute不接收参数使用
        /// </summary>
        /// <param name="execute"></param>
        /// <param name="canExecute"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public RelayCommand(Action<T> execute, Func<bool> canExecute)
        {
            _execute = execute ?? throw new ArgumentNullException(nameof(execute));
            _canExecute2 = canExecute;
        }


        [DebuggerStepThrough]
        public bool CanExecute(object parameter)
        {
            if (_canExecute == null && _canExecute2 == null) return true;
            else if (_canExecute2 != null) return _canExecute2();
            else
            {
                return typeof(T) == typeof(object) ? _canExecute(((T)parameter)) : _canExecute((T)Convert.ChangeType(parameter, typeof(T)));
            }
        }

        public event EventHandler CanExecuteChanged
        {
            add
            {
                if (_canExecute != null || _canExecute2 != null)
                    CommandManager.RequerySuggested += value;
            }
            remove
            {
                if (_canExecute != null || _canExecute2 != null)
                    CommandManager.RequerySuggested -= value;
            }
        }

        public void Execute(object parameter)
        {
            //传递多个值时，使用下面转换方式会报错，因此需要单独处理
            if (typeof(T) == typeof(object))
                _execute((T)parameter);
            else
                _execute((T)Convert.ChangeType(parameter, typeof(T)));
        }
    }
}